package com.wisecoach.gatewayplus.spring.seata;

import com.wisecoach.gatewayplus.transaction.TransactionAttributeSource;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.Ordered;

/**
 * {@code @author:} wisecoach
 * {@code @date:} 2023/9/29 下午2:21
 * {@code @version:} 1.0.0
 */
public class TCCChaincodeTxInterceptor implements MethodInterceptor, Ordered {

    private static final int DEFAULT_PRIORITY = 10;
    private final TCCLogicExecutor tccLogicExecutor;
    private final TransactionAttributeSource txAttrSource;

    public TCCChaincodeTxInterceptor(TCCLogicExecutor tccLogicExecutor, TransactionAttributeSource txAttrSource) {
        this.tccLogicExecutor = tccLogicExecutor;
        this.txAttrSource = txAttrSource;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        return tccLogicExecutor.tccTry(invocation);
    }

    @Override
    public int getOrder() {
        return DEFAULT_PRIORITY;
    }

    public TransactionAttributeSource getTransactionAttributeSource() {
        return txAttrSource;
    }
}
